import { defineStore } from 'pinia';

export const videoStore = defineStore({
  id: 'video-player',
  state: () => ({
    playerlist: {},
    playerSet: new Set()
  }),
  getters: {

  },
  actions: {
    // 添加当前播放视频实例到播放队列中
    add(id, instance) {
      this.playerSet.add(id)
      this.playerlist[id] = instance
    },
    // 从播放队列中删除一个视频
    del(id) {
      this.playerSet.delete(id)
      let dom = document.querySelector(`#video_instance_${id}`)
      dom && document.querySelector('#video_container')?.removeChild(dom)
      delete this.playerlist[id]
    },
    // 暂停某个视频
    pause(id) { },
    // 暂停所有视频
    pauseAll() { },
    // 播放某个视频
    play(id) { },
    // 播放所有视频
    playAll() { },
    // 查询当前播放列表是否有当前设备
    has(id) {
      return this.playerSet.has(id)
    },
    // 清空系统所有视频
    removeAll() {
      this.playerSet.clear()
      for (let key in this.playerlist) {
        this.playerlist[key].closeDialog()
        const deviceId = key.split("id=")[1]
        let dom = document.querySelector(`#video_instance_${deviceId}`)
        dom && document.querySelector('#video_container')?.removeChild(dom)
      }
      this.playerlist = {}
      var element = document.querySelector('.video_container');
      if (element) element.innerHTML = ""
    }
  },
});
